import 'package:citiesocial/_include.dart';
import 'package:package_info/package_info.dart';

class Settings extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _MySettingsState();
}

class _MySettingsState extends BaseState<Settings> {
  CustomerServiceEntity _urls;
  String _version;
  void _onTap(int index) {
    switch (index) {
      case 0:
        {
          if (_urls?.service_describe != null) {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (_) => WebviewScaffold(
                      appBar: AppBarUtils.normal(context, '服务说明'),
                      url: '${_urls.service_describe}',
                    ),
              ),
            );
          }
        }
        break;
      case 1:
        {
          if (_urls?.preferential_info != null) {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (_) => WebviewScaffold(
                      appBar: AppBarUtils.normal(context, '优惠说明'),
                      url: '${_urls.preferential_info}',
                    ),
              ),
            );
          }
        }
        break;
      case 2:
        {
          if (_urls?.about_us != null) {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (_) => WebviewScaffold(
                      appBar: AppBarUtils.normal(context, '关于我们'),
                      url: '${_urls.about_us}',
                    ),
              ),
            );
          }
        }
        break;
      case 3:
        {}
        break;
      // case 4:
      //   {}
      //   break;
      // case 5:
      //   {}
      //   break;
      default:
        break;
    }
  }

  void _logout() {
    Configs.saveUserState(null);
    Configs.saveCartCount(0);
    Toast.show(context, '退出成功').then((_) {
      Navigator.pop(context, '-1');
    });
  }

  void _getInfo() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      //String appName = packageInfo.appName;
      //String packageName = packageInfo.packageName;
      String version = packageInfo.version;
      if (version != null && version.length > 0) {
        setState(() {
          _version = version;
        });
      }
      //String buildNumber = packageInfo.buildNumber;
    });
  }

  void _loadUrls() {
    LoadingDialog.show(context);
    HttpManager.customerService(context: context).then((urls) {
      _urls = urls;
      LoadingDialog.dismiss(context);
    });
  }

  @override
  void initState() {
    _getInfo();
    Future.delayed(Configs.loadDelay, () {
      _loadUrls();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBarUtils.normal(context, '设置'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: ListView(
            children: <Widget>[
              ItemMenu(
                '服务说明',
                rightIcon: 'imgs/js_tabs_profile_img_more_g.png',
                onTap: () => _onTap(0),
              ),
              ItemMenu(
                '订阅最新优惠信息',
                rightIcon: 'imgs/js_tabs_profile_img_more_g.png',
                onTap: () => _onTap(1),
              ),
              ItemMenu(
                '关于我们',
                rightIcon: 'imgs/js_tabs_profile_img_more_g.png',
                onTap: () => _onTap(2),
              ),
              ItemMenu(
                '地区设定',
                rightIcon: 'imgs/js_tabs_profile_img_more_g.png',
                onTap: () => _onTap(3),
              ),
              GestureDetector(
                child: Container(
                  width: double.infinity,
                  height: 50.0,
                  margin: EdgeInsets.symmetric(vertical: 50.0),
                  child: Center(
                    child: Text(
                      '退出',
                      style: TextStyle(color: Colors.red, fontSize: 18.0),
                    ),
                  ),
                  color: Colors.white,
                ),
                onTap: _logout,
              ),
              Container(
                margin: EdgeInsets.only(top: 70.0),
                child: Center(
                  child: Text('v$_version',
                      style: TextStyle(color: Colors.grey, fontSize: 12.0)),
                ),
              ),
            ],
          ),
        ),
      );
}
